package com.zxn.school;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author xiyu
 * @version 1.0
 * @date 2025/3/22 14:11
 * @description
 */
public class Dao {
    ArrayList<Student1> student1s = new ArrayList<>();

    ArrayList<Teacher>teachers = new ArrayList<>();
    Scanner input = new Scanner(System.in);
    static Dao dao = new Dao();


    {//预创建
        teachers.add(new Teacher(1,"张老师","语文"));
        teachers.add(new Teacher(2,"李老师","数学"));

        student1s.add(new Student1(1,"李大","男",44,1));
        student1s.add(new Student1(2,"李二","男",36,2));
        student1s.add(new Student1(3,"李三","男",87,2));
    }
    //1.添加老师
    public void addTeacher(){
        System.out.print("请输入添加老师的编号：");
        int tid = input.nextInt();
        for (Teacher teacher : teachers) {
            if (tid==teacher.getTid()){
                System.err.println("此id已被占用");
                return;
            }
        }
        System.out.print("请输入添加老师的姓名：");
        String tname = input.next();
        System.out.print("请输入添加老师的科目：");
        String subject = input.next();
        Teacher teacher = new Teacher(tid,tname,subject);
        teachers.add(teacher);
        System.out.println("=====添加完成=====");

    }
    //2.添加学生
    public void addStudent(){

        System.out.print("请输入添加学生的编号：");
        int sid = input.nextInt();
        for (Student1 student1 : student1s) {
            if (sid== student1.getSid()){
                System.err.println("此id已被占用");
                return;
            }
        }
        System.out.print("请输入添加学生的姓名：");
        String name = input.next();
        System.out.print("请输入添加学生的性别：");
        String sex = input.next();
        System.out.print("请输入添加学生的成绩：");
        double score = input.nextDouble();
        System.out.print("请输入添加学生所属的老师id：");
        int tid = input.nextInt();
        Student1 student1 = new Student1(sid, name, sex, score, tid);
        student1s.add(student1);
        System.out.println("=====添加完成=====");
    }
    //3.查询所有学生(隐藏老师id)
    public void findAllstu(){
        for (Student1 student1 : student1s) {
            System.out.println("id:"+ student1.getSid()+
                    "，姓名："+ student1.getName()+
                    "，性别："+ student1.getSex()+
                    "，成绩："+ student1.getScore());
        }
        System.out.println("--------------------------");
    }
    //4.查询老师各自管理了多少学生
    public void findStuofTea(){
        System.out.print("请输入要查询的老师的id：");
        int tid = input.nextInt();
        int count = 0;
        for (Student1 student1 : student1s) {
            if (tid== student1.getTid()){
                count++;
            }
        }
        System.out.println("该老师管理了："+count+"个学生");
    }
    //5.查询学生属于哪个老师管理
    public void findTeaofStu(){
        System.out.print("请输入要查询的学生的id：");
        int sid = input.nextInt();
        int tid = 0;
        for (Student1 student1 : student1s) {
            if (sid == student1.getSid()){
                tid = student1.getTid();
            }
        }
        for (Teacher teacher : teachers) {
            if (tid==teacher.getTid()){
                System.out.println("该学生属于"+teacher.getTname()+"老师管理");
            }
        }
    }
    //6.查询不及格的学生(<60)
    public void findFail(){
        for (Student1 student1 : student1s) {
            if (student1.getScore()<60){
                System.out.println(student1.getName()+"不及格");
            }
        }
    }
    //7.从高到低排列学生的成绩
    public void StuscoreforTea(){
        double[] stusc = new double[student1s.size()];
        for (int i = 0; i < student1s.size(); i++) {
            stusc[i]= student1s.get(i).getScore();
        }
        for (int i = 0; i < stusc.length; i++) {
            for (int j = 0; j < stusc.length; j++) {
                if (stusc[i]>stusc[j]){
                    double temp = stusc[i];
                    stusc[i] = stusc[j];
                    stusc[j] = temp;
                }
            }
        }
        ArrayList<Student1> student1Score = new ArrayList<>();
        for (int i = 0; i < stusc.length; i++) {
            for (Student1 student1 : student1s) {
                if (stusc[i]== student1.getScore()){
                    student1Score.add(student1);
                }
            }
        }
        for (Student1 student1 : student1Score) {
            System.out.println("id:"+ student1.getSid()+
                    "，姓名："+ student1.getName()+
                    "，性别："+ student1.getSex()+
                    "，成绩："+ student1.getScore());
        }




    }
    //8.修改学生成绩
    public void editStuscore(){
        System.out.print("输入需要修改成绩的学生id：");
        int sid = input.nextInt();
        for (Student1 student1 : student1s) {
            if (student1.getSid()==sid){
                System.out.print("输入修改后的成绩：");
                double score = input.nextDouble();
                student1.setScore(score);
            }
        }
    }
    //9.开除学生
    public void reStu(){
        System.out.print("输入需要开除的学生id：");
        int sid = input.nextInt();
        for (int i = 0; i < student1s.size(); i++) {
            if (sid== student1s.get(i).getSid()){
                student1s.remove(i);
                i--;
            }
        }
    }



}
